package com.cdtu.room.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.cdtu.room.domain.Calendar;
import com.cdtu.room.dto.ReservationDto;
import com.cdtu.room.mapper.CalendarMapper;
import com.cdtu.room.vo.ReservationVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cdtu.room.mapper.ReservationMapper;
import com.cdtu.room.domain.Reservation;
import com.cdtu.room.service.IReservationService;

/**
 * 预约信息Service业务层处理
 * 
 * @author hcc
 * @date 2024-02-27
 */
@Service
public class ReservationServiceImpl implements IReservationService 
{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    /**
     * 查询预约信息
     * 
     * @param reservationId 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationByReservationId(Long reservationId)
    {
        return reservationMapper.selectReservationByReservationId(reservationId);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setUserId(SecurityUtils.getUserId());
        reservation.setCreateTime(DateUtils.getNowDate());
        reservation.setBookingTime(DateUtils.getNowDate());
        reservation.setCreateBy(SecurityUtils.getUsername());
        int i = reservationMapper.insertReservation(reservation);
        if (i == 1){
            SysNotice sysNotice = new SysNotice();
            sysNotice.setStatus("0");
            sysNotice.setNoticeType("1");
            sysNotice.setNoticeTitle("有新的预约，请及时处理");
            sysNoticeMapper.insertNotice(sysNotice);
        }
        return i;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        if (reservation.getStatus() == 1){
            Reservation reservation1 = reservationMapper.selectReservationByReservationId(reservation.getReservationId());
            Calendar calendar = new Calendar();
            calendar.setReservationId(reservation.getReservationId());
            calendar.setDate(DateUtils.getNowDate());
            calendar.setStatus(0);
            calendar.setRoomId(reservation1.getRoomId());
            calendarMapper.insertCalendar(calendar);
            SysNotice sysNotice = new SysNotice();
            sysNotice.setStatus("0");
            sysNotice.setNoticeType("1");
            sysNotice.setNoticeTitle("预约信息已通过，请及时查看");
            sysNoticeMapper.insertNotice(sysNotice);
        }
        reservation.setUpdateTime(DateUtils.getNowDate());
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param reservationIds 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByReservationIds(Long[] reservationIds)
    {
        return reservationMapper.deleteReservationByReservationIds(reservationIds);
    }

    /**
     * 删除预约信息信息
     * 
     * @param reservationId 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByReservationId(Long reservationId)
    {
        return reservationMapper.deleteReservationByReservationId(reservationId);
    }

    /**
     * 校验预约时间段是否冲突
     * @param reservation
     * @return
     */
    @Override
    public List<Reservation> checkByTime(Reservation reservation) {
        return reservationMapper.checkByTime(reservation);
    }

    /**
     * 查询预约信息列表
     * @param reservationVo
     * @return
     */
    @Override
    public List<ReservationDto> getReservationList(ReservationVo reservationVo) {
        return reservationMapper.getReservationList(reservationVo);
    }
}
